<%@ WebHandler Language="C#" Class="Portlet3" %>

using System;
using System.Web;
using System.Threading;
using System.Text;
using System.Collections.Generic;
using System.Collections;
using System.Web.SessionState;
 

public class Portlet3 : IHttpHandler, IReadOnlySessionState{
 
    String fechainicio = "";
    String fechafin = "";
    String ColorAc = "";
    String Usuario = "";
    String Grafico = "";
    StringBuilder sb = new StringBuilder();
    String ValTot;
    public void ProcessRequest (HttpContext context) {
         
         context.Response.ContentType = "text/plain";
         List<BEGenGrafico> lista = null;

         fechainicio = Utils.getStringFechaYYMMDDHHMM(context.Request["fechainicio"]) + " 00:00:00";
         fechafin = Utils.getStringFechaYYMMDDHHMM(context.Request["fechafin"]) + " 23:59:00";
         Usuario = context.Request["Usuario"];
         
        
        lista = DBPrincipal.getActividadesRealizadas(fechainicio, fechafin, Usuario, Grafico);

        if (lista.Count != 0)
        {
            sb = getConstruccionBarra(lista);
        }
        else
        {
            sb = getCargaInicial();
        }
     
            
         

        context.Response.Write(sb.ToString()); 
    }



    public StringBuilder getConstruccionBarra(List<BEGenGrafico> lista) 
    {


        if (lista != null && lista.Count > 0)
        {
           
           Int64 i = 0; 
           String var1 = "<graph rotateNames='1' caption='' xAxisName='" + IdiomaCultura.getMensajeEncodeHTML(IdiomaCultura.WEB_USUARIO) + "' yAxisName='" + IdiomaCultura.getMensajeEncodeHTML(IdiomaCultura.WEB_ACTIVIDAD) + "' decimalPrecision='0' formatNumberScale='0'> "; 
            
           foreach (BEGenGrafico bean in lista)
           {

                   ValTot = "";
                   string var = i == 0 ? var1 : "/>";
                   string varF = i == lista.Count - 1 ? "/></graph>" : "";

                   if (!bean.Total.Equals("0"))
                   {
                       ValTot = bean.Total;
                   }

                   ColorAc = coloress(i);



                   sb.Append(String.Format(var + "{0} {1} {2} {3}" + varF,
                        
                    "<set name='" + HttpUtility.HtmlEncode(bean.avrusuario) + "'",
                    "hoverText='" + HttpUtility.HtmlEncode(bean.usuario) + "'",
                    "value='" + HttpUtility.HtmlEncode(ValTot) + "'",
                    "color='" + HttpUtility.HtmlEncode(ColorAc) + "'"
                
                
                   ));
  
                   i++;
            }
       }
    return sb;

    }


    public StringBuilder getCargaInicial()
    {
   
        
        sb.Append("<graph caption='' xAxisName='' yAxisName='' decimalPrecision='0' formatNumberScale='0'> ");
        sb.Append("  <set name='' value='' color='' /> ");
        //sb.Append("  <set name='Ene' value='671' color='8B3a00' /> ");
        //sb.Append("  <set name='Feb' value='671' color='8B2300' /> ");
        //sb.Append("  <set name='Mar' value='671' color='8BBA00' /> ");
        //sb.Append("  <set name='Apr' value='494' color='FF8E46'/> ");
        //sb.Append("  <set name='May' value='761' color='008E8E'/> ");
        //sb.Append("  <set name='Jun' value='960' color='D64646'/> ");
        //sb.Append("  <set name='Jul' value='629' color='8E468E'/> ");
        //sb.Append("  <set name='Aug' value='622' color='588526'/> ");
        //sb.Append("  <set name='Sep' value='376' color='B3AA00'/> ");
        //sb.Append("  <set name='Oct' value='494' color='008ED6'/> ");
        //sb.Append("  <set name='Nov' value='761' color='9D080D'/> ");
        //sb.Append("  <set name='Dec' value='960' color='A186BE'/> ");
        sb.Append("  </graph> ");

        return sb;
    }
    

 
    public bool IsReusable {
        get {
            return false;
        }
    }

    public String coloress(Int64 lis)
    {
        List<String> Lista = new List<String>();
        String color="";
        
        Lista.Add("AFD8F8");
        Lista.Add("F6BD0F");
        Lista.Add("8BBA00");
        Lista.Add("FF8E46");
        Lista.Add("008E8E");
        Lista.Add("D64646");
        Lista.Add("8E468E");
        Lista.Add("588526");
        Lista.Add("B3AA00");
        Lista.Add("008ED6");

        int i = Convert.ToInt16(lis) + 1;
        color = Lista[i % 10];
        
        //Random Aleatorios = new Random();
        //int Red = Aleatorios.Next(0,255);
        //int Blue = Aleatorios.Next(0,255);
        //int Green = Aleatorios.Next(0,255);
        //int alpha = Convert.ToInt16(i) + 1;

        //System.Drawing.Color c = System.Drawing.Color.FromArgb(alpha, Red, Green, Blue);
        //String Color2b = String.Format("#{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B);        

        return color;
        
    }
    
    
    

}